import {Component, OnInit} from "@angular/core";
import {ActivatedRoute, Router} from "@angular/router";
import {NzMessageService} from "ng-zorro-antd";
import {FetchService} from "../../../core/net/fetch.service";

@Component({
  selector: 'app-history-versions',
  templateUrl: './history-versions.component.html',
  styleUrls: ['./history-versions.component.scss']
})
export class HistoryVersionsComponent implements OnInit {
  baseUrl = (window as any).environment.apiBaseUrl;
  pdfSrc = '';
  listData = [];
  selectedItemId;
  projectId;
  num;

  constructor(
    private message: NzMessageService,
    private fetchService: FetchService,
    private routerInfo: ActivatedRoute,
    private router: Router
  ) {
    this.projectId = this.routerInfo.snapshot.queryParams['projectId'];
    this.num = this.routerInfo.snapshot.queryParams['num'];
  }

  ngOnInit(): void {
    this.queryList();
  }

  queryList() {
    this.fetchService.post<any>('archives/wrsProjectFileVersion/list', {projectId: this.projectId, num: this.num})
      .subscribe(res => {
        this.listData = res;
       /* this.listData = [
          {id: 1, version: '1.1', fileId: '390b7aeb92e84038a7f3e206c6db4695', createDate: '2020-03-18T00:00:00.000+0000'},
          {id: 2, version: '1.2', fileId: '390b7aeb92e84038a7f3e206c6db4695', createDate: '2020-03-18T00:00:00.000+0000'},
          {id: 3, version: '1.3', fileId: '390b7aeb92e84038a7f3e206c6db4695', createDate: '2020-03-18T00:00:00.000+0000'},
          {id: 4, version: '1.4', fileId: '390b7aeb92e84038a7f3e206c6db4695', createDate: '2020-03-18T00:00:00.000+0000'},
          {id: 5, version: '1.5', fileId: '390b7aeb92e84038a7f3e206c6db4695', createDate: '2020-03-18T00:00:00.000+0000'},
        ];*/
        if (this.listData.length > 0) {
         this.changePdf(this.listData[0]);
        }
      });
  }

  changePdf(item): void {
    this.selectedItemId = item.id;
    this.pdfSrc = `${(window as any).environment.apiBaseUrl}comm/file/view/${item.fileId}`;
  }

  downloadPdf(): void {
    $('#download').trigger('click');
  }

  back(): void {
    this.router.navigate(['/project-files/files-version-list']);
  }
}
